package org.spring.boot.jdbctemplate.util;

import org.spring.boot.jdbctemplate.dao.GenerationDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: eastcom
 * @desc:
 **/
public abstract class AbstractGeneration {

	@Autowired
	private GenerationDao generationDao;

	private String sftpUrl;

	private String fileDir;

	/**
	 * 解析文件
	 * @param file 解析的文件
	 * @return 解析文件后的数据
	 * @throws Exception 异常
	 */
	@SuppressWarnings("resource")
	public List<String[]> parase(File file) throws Exception {
		List<String[]> list = new ArrayList<>();
		BufferedReader reader;
		FileInputStream fis = new FileInputStream(file);
		String encode = "UTF-8";
		InputStreamReader isr = new InputStreamReader(fis, encode);
		reader = new BufferedReader(isr);
		reader.readLine();
		String lineString;
		while (!StringUtils.isEmpty(lineString = reader.readLine())) {
			String[] array = lineString.split(",",-1);
			// 时间为必填项，判断条件
			if(array.length == 0){
				continue;
			}
			if (!StringUtils.isEmpty(array[0])) {
				list.add(array);
			}
		}
		return list;
	}

	/**
	 * 数据入库
	 * @param list 添加数据
	 * @throws Exception 返回异常信息
	 */
	public abstract void save(List<String[]> list) throws Exception;
 
	public String getSftpUrl() {
		return sftpUrl;
	}

	public String getFileDir() {
		return fileDir;
	}

	public GenerationDao getGenerationDao() {
		return generationDao;
	}

	public void setGenerationDao(GenerationDao generationDao) {
		this.generationDao = generationDao;
	}

	public void setSftpUrl(String sftpUrl) {
		this.sftpUrl = sftpUrl;
	}

	public void setFileDir(String fileDir) {
		this.fileDir = fileDir;
	}
}
